package com.ruoyi.crm.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.auth.AuthUtil;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.crm.domain.CrmCompany;
import com.ruoyi.crm.service.ICrmCompanyService;
import com.ruoyi.system.api.RemoteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.crm.domain.CrmLinkman;
import com.ruoyi.crm.service.ICrmLinkmanService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 客户关系管理_联系人Controller
 * 
 * @author ruoyi
 * @date 2022-01-28
 */
@RestController
@RequestMapping("/linkman")
public class CrmLinkmanController extends BaseController
{
    @Autowired
    private ICrmLinkmanService crmLinkmanService;

    @Autowired
    private ICrmCompanyService crmCompanyService;

    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 查询客户关系管理_联系人列表
     */
    @RequiresPermissions("crm:linkman:list")
    @GetMapping("/list")
    public TableDataInfo list(CrmLinkman crmLinkman)
    {
        startPage();
        List<CrmLinkman> list = crmLinkmanService.selectCrmLinkmanList(crmLinkman);
        return getDataTable(list);
    }

    /**
     * 导出客户关系管理_联系人列表
     */
    @RequiresPermissions("crm:linkman:export")
    @Log(title = "客户关系管理_联系人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmLinkman crmLinkman)
    {
        List<CrmLinkman> list = crmLinkmanService.selectCrmLinkmanList(crmLinkman);
        ExcelUtil<CrmLinkman> util = new ExcelUtil<CrmLinkman>(CrmLinkman.class);
        util.exportExcel(response, list, "客户关系管理_联系人数据");
    }

    /**
     * 获取客户关系管理_联系人详细信息
     */
    @RequiresPermissions("crm:linkman:query")
    @GetMapping(value = "/{linkmanId}")
    public AjaxResult getInfo(@PathVariable("linkmanId") Long linkmanId)
    {
        return AjaxResult.success(crmLinkmanService.selectCrmLinkmanByLinkmanId(linkmanId));
    }

    /**
     * 新增客户关系管理_联系人
     */
    @RequiresPermissions("crm:linkman:add")
    @Log(title = "客户关系管理_联系人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmLinkman crmLinkman, HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        crmLinkman.setUserName(JwtUtils.getUserName(token));
        crmLinkman.setUserId(Long.valueOf(JwtUtils.getUserId(token)));

        return toAjax(crmLinkmanService.insertCrmLinkman(crmLinkman));
    }
    /**
     * 查询联系人列表
     */
    @GetMapping("/getlinkmen")
    public AjaxResult getlinkmen(CrmLinkman crmLinkman)
    {
        return AjaxResult.success(crmLinkmanService.selectCrmLinkmanList(crmLinkman));
    }
    /**
     * 修改客户关系管理_联系人
     */
    @RequiresPermissions("crm:linkman:edit")
    @Log(title = "客户关系管理_联系人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmLinkman crmLinkman)
    {
        return toAjax(crmLinkmanService.updateCrmLinkman(crmLinkman));
    }

    /**
     * 删除客户关系管理_联系人
     */
    @RequiresPermissions("crm:linkman:remove")
    @Log(title = "客户关系管理_联系人", businessType = BusinessType.DELETE)
	@DeleteMapping("/{linkmanIds}")
    public AjaxResult remove(@PathVariable Long[] linkmanIds)
    {
        return toAjax(crmLinkmanService.deleteCrmLinkmanByLinkmanIds(linkmanIds));
    }

    @Log(title = "联系人", businessType = BusinessType.DELETE)
    @GetMapping("/getCompanies")
    public AjaxResult getCompanies()
    {
        AjaxResult ajax = AjaxResult.success();
        List<CrmCompany> list = crmCompanyService.selectCrmCompanyList(new CrmCompany());

        ajax.put("companies",list);
        return ajax;

    }
}
